package com.yf1706.service;

import java.util.Optional;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.CountIncomeTaxRate;
import com.yf1706.dao.jpa.ICountIncomeTaxRateJDAO;

/**
 * 
 * @author 锦涛
 *
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class TaxRatesService {
	@Autowired
	private ICountIncomeTaxRateJDAO taxRateJDAO;
	
	//增加税率
	@Transactional
	public void save(CountIncomeTaxRate cRate) {
		taxRateJDAO.save(cRate);
	}
	
	//根据ID删除税率
	@Transactional
	public void remove(String steps) {
		for(String step : steps.split(",") ) {
			if ( step != null) {
				taxRateJDAO.deleteById(Integer.parseInt(step));
			}
		}
	}
	
	/**
	 * 修改数据
	 * @param cRate
	 * 先根据要修改的对象id查询出对应的持久化对象
	 * 直接调用持久化对象的set方法修改对象的数据
	 */
	@Transactional
	public void update(CountIncomeTaxRate cRate) {
		Optional<CountIncomeTaxRate> sessionCRate = taxRateJDAO.findById(cRate.getStep());
		sessionCRate.get().setStepMin(cRate.getStepMin());
		sessionCRate.get().setStepMax(cRate.getStepMax());
		sessionCRate.get().setTaxRates(cRate.getTaxRates());
		sessionCRate.get().setQuickDeduction(cRate.getQuickDeduction());
		sessionCRate.get().setCutoffPoint(cRate.getCutoffPoint());
	}
	
	//分页查询税率
	@Transactional
	public Page<CountIncomeTaxRate> querybypage(int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return taxRateJDAO.findAll(pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return taxRateJDAO.findAll(pageable);
		}
	}
	
}
